package com.servicio.common.UI;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class PresentarWindow extends JInternalFrame {

	TableRender table;
	public JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PresentarWindow frame = new PresentarWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public TableRender getTable() {
		return table;
	}

	/**
	 * Create the frame.
	 */

	public PresentarWindow() {
		setIconifiable(true);
		setMaximizable(true);
		setResizable(true);
		setClosable(true);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(
				new FormLayout(
						new ColumnSpec[] { ColumnSpec.decode("434px"), },
						new RowSpec[] { RowSpec.decode("271px"), }));

		JScrollPane scrollPane = new JScrollPane();
		getContentPane().add(scrollPane, "1, 1, fill, fill");

		table = new TableRender();
		table.setOpaque(true);
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		scrollPane.setViewportView(table);

		textField = new JTextField();
		textField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				filterContent();
			}
		});
		scrollPane.setColumnHeaderView(textField);
		textField.setColumns(10);

		// this.pack();
	}

	public void getSelection(){
		Vector<String> selected = new Vector<String>();
		for(int i = 0; i < table.getRowCount(); i++)
			if((Boolean) table.getValueAt(i, 4))
				selected.add((String) table.getValueAt(i, 0));
		System.out.println(selected);	
	}

	public void filterContent() {
		getSelection();
		String target = textField.getText();
		for (int row = 0; row < table.getRowCount(); row++)
			for (int col = 0; col < table.getColumnCount() - 1; col++) {
				String next = (String) table.getValueAt(row, col);
				if (next.contains(target)) {
					// showSearchResults(row, col);
					table.setCellColor(row, col);
					System.out.println(row + " " + col);

				}
			}
	}

}
